package whr.modules.assets.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import whr.common.utils.PageUtils;
import whr.common.utils.Query;

import whr.modules.assets.dao.InfoDao;
import whr.modules.assets.entity.InfoEntity;
import whr.modules.assets.service.InfoService;


@Service("infoService")
public class InfoServiceImpl extends ServiceImpl<InfoDao, InfoEntity> implements InfoService {
    @Autowired
    private InfoService infoService;
    @Autowired
    private InfoDao infoDao;


    /**
     *  '全部' null
     *  0  待用
     *  1 领用申请中
     *  2 使用中
     *  3 维修中
     *  4 报废
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InfoEntity> page = this.page(
                new Query<InfoEntity>().getPage(params),
                new QueryWrapper<InfoEntity>()
                        .like(StringUtils.isNotBlank((String) params.get("key")),"assets_name",params.get("key"))
                        .eq(StringUtils.isNotBlank((String) params.get("key2")),"assets_status",params.get("key2"))
                        .orderByAsc("assets_status")
        );
        return new PageUtils(page);
    }

    /**
     * 判断资产编号是否存在，存在就不能添加
     * @param info
     */

    @Override
    public void saveByAssetsStatus(InfoEntity info) {
        String assetsNum=info.getAssetsNum();
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assets_num",assetsNum);
        InfoEntity one = infoService.getOne(queryWrapper);
        if(one!=null){
            throw new RuntimeException("资产编号已经存在，请修改");
        }else{
            infoService.save(info);
        }

    }


    @Override
    public Object getAssetsCount() {
        return infoDao.getAssetsCount();

    }


    /**
     * 饼图，名字不重复加数量算占比
     *
     * @return
     */
    @Override
    public List<HashMap<String, Object>> pieList() {
        List<HashMap<String,Object>> pieList= infoDao.pieList();
       /* for(HashMap<String,Object> item : pieList){
                System.out.println("资产名字"+item.get("assets_name")+"数量"+item.get("asset_count"));

        }*/
        return pieList;
    }


}